package com.xiaoman.read.readui;

import com.xiaoman.read.config.Config;
import com.xiaoman.read.file.FileFactory;
import javax.swing.*;
import java.awt.*;



public class ReadUI {

    private JPanel mainPanel;
    private JTextPane textContent;
    private JScrollPane scrollPane;

    private JButton nextChapterButton;
    private JButton previousChapterButton;

    public ReadUI() {
        // 初始化 mainPanel
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout()); // 使用 BorderLayout 布局

        // 初始化 textContent
        textContent = new JTextPane();
        textContent.setEditable(false); // 如果不需要编辑，设置为不可编辑
        // 将 textContent 放入 JScrollPane
        scrollPane = new JScrollPane(textContent);

        // 初始化按钮
        previousChapterButton = new JButton("Previous Chapter");
        nextChapterButton = new JButton("Next Chapter");

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(previousChapterButton);
        buttonPanel.add(nextChapterButton);

        // 将按钮面板添加到 mainPanel 的顶部
        mainPanel.add(buttonPanel, BorderLayout.NORTH);

        // 将 scrollPane 添加到 mainPanel 的中心
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 添加按钮事件监听器
        previousChapterButton.addActionListener(e -> {
            int currentIndex = Config.getCurrentChapterIndex();
            if (currentIndex > 0) {
                Config.setCurrentChapterIndex(currentIndex - 1);
                new FileFactory().displayCurrentChapter();
            }
        });

        nextChapterButton.addActionListener(e -> {
            int currentIndex = Config.getCurrentChapterIndex();
            Config.setCurrentChapterIndex(currentIndex + 1);
            if (Config.getFileType().equalsIgnoreCase("epub") && currentIndex < Config.chapters.size() - 1) {
                new FileFactory().displayCurrentChapter();
            } else if (Config.getFileType().equalsIgnoreCase("pdf") && currentIndex < Config.chapters.size() - 1) {
                new FileFactory().displayCurrentChapter();

            }
        });
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    public JTextPane getTextContent() {
        return textContent;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }
}
